/**
给定一个整数数据流和一个窗口大小，根据该滑动窗口的大小，计算其所有整数的移动平均值。

示例:

MovingAverage m = new MovingAverage(3);
m.next(1) = 1
m.next(10) = (1 + 10) / 2
m.next(3) = (1 + 10 + 3) / 3
m.next(5) = (10 + 3 + 5) / 3


来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/moving-average-from-data-stream
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
*/
type MovingAverage struct {
    Size    int   `json:"size"`    //滑动窗口大小
	Element []int `json:"element"` //存储元素
	Move    int   `json:"move"`    //当前位置
    Total   int   `json:"total"`   //当前元素和
}


/** Initialize your data structure here. */
func Constructor(size int) MovingAverage {
    ret := MovingAverage{}
	ret.Size = size
	ret.Element = make([]int, 0, size)
	return ret
}


func (this *MovingAverage) Next(val int) float64 {
    if len(this.Element) < cap(this.Element) {
		this.Element = append(this.Element, val)
		this.Total += val

	} else {
		this.Total -= this.Element[this.Move]
		this.Total += val
		this.Element[this.Move] = val
		this.Move += 1
		this.Move %= this.Size
	}

	return float64(this.Total) / float64(len(this.Element))
}


/**
 * Your MovingAverage object will be instantiated and called as such:
 * obj := Constructor(size);
 * param_1 := obj.Next(val);
 */